package com.gyms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.classic.okhttp.beans.HVVenueOrderFieldBean;
import com.gyms.R;
import com.gyms.activity.OrderDetailActivity;
import com.gyms.manager.RevertManger;

import java.util.ArrayList;
import java.util.List;

import utils.Arith;
import utils.DateUtil;
import utils.DecimalUtils;
import utils.StringUtils;

/**
 * ltx on 2016/12/7 14:01
 * 退款列表的adapter
 */
public class RefundAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private RefundHolder mViewHolder;
    private ArrayList<HVVenueOrderFieldBean> orderField;//需要退单的list
    private List<HVVenueOrderFieldBean> mSelectedRefund=new ArrayList<>();//已经选择的退单的list
    private final static int sSHELCTED=1;
    private final static int sNOSHELCTED=0;
    private ChangePrice mIfChangePrice;
    public double mAllRefundPrice;//退款价格
    private OrderDetailActivity mOrderDetailActivity;
    private RevertManger mRevertManger;

    private double mSingleBackMonery;//返还的价格
    public double mDisPrice;//扣减的价格
    private double mSingDisPrice;//单个扣减的价格

    public interface ChangePrice{
        void refumdPrice(double pirce);//退款的金额
        void refumdList(List<HVVenueOrderFieldBean> mSelectedRefund);//退款的id
        void refefunDisPrice(double mDisPrice);//扣减的金额
    }

    public void setChangePrice(ChangePrice mIfChangePrice){
        this.mIfChangePrice=mIfChangePrice;
    }

    public RefundAdapter(Context context) {
        mContext = context;
        mOrderDetailActivity= (OrderDetailActivity) context;
        mRevertManger=mOrderDetailActivity.mRevertManger;
    }

    @Override

    public int getCount() {
        return StringUtils.isNull(orderField)?0:orderField.size();
    }

    @Override
    public HVVenueOrderFieldBean getItem(int position) {
        return orderField.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.order_refund_item, parent, false);
            mViewHolder=new RefundHolder();
            mViewHolder.mTvOrderTime= (TextView) convertView.findViewById(R.id.tv_order_time);
            mViewHolder.mTvOrderWeek= (TextView) convertView.findViewById(R.id.tv_order_week);
            mViewHolder.mTvOrderDay= (TextView) convertView.findViewById(R.id.tv_order_date);
            mViewHolder.mTvOrderFieldName= (TextView) convertView.findViewById(R.id.tv_field_name);
            mViewHolder.mTvSinglePrice= (TextView) convertView.findViewById(R.id.tv_single_price);
            mViewHolder.mCbCheck= (CheckBox) convertView.findViewById(R.id.cb_zhi);
            mViewHolder.mCbCheck.setOnClickListener(this);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder= (RefundHolder) convertView.getTag();
        }
        mViewHolder.mCbCheck.setTag(position);
        HVVenueOrderFieldBean mHVVenueOrderFieldBean=orderField.get(position);
        dofullView(mHVVenueOrderFieldBean);

        return convertView;
    }

    /**
     * 填充view
     * @param mHVVenueOrderFieldBean 子订单的对象
     */

    private void dofullView(HVVenueOrderFieldBean mHVVenueOrderFieldBean) {
        mViewHolder.mTvOrderFieldName.setText(mHVVenueOrderFieldBean.getFieldName());
        mViewHolder.mTvOrderWeek.setText(DateUtil.getWeek(mHVVenueOrderFieldBean.getStartTime()));
        mViewHolder.mTvOrderDay.setText(DateUtil.stampToDate(mHVVenueOrderFieldBean.getStartTime()));
        mViewHolder.mTvOrderTime.setText(DateUtil.intToHour(mHVVenueOrderFieldBean.getStartTime()) + "-" + DateUtil.intToHour(mHVVenueOrderFieldBean.getEndTime()));
//        DecimalUtils.setPriceText(mContext,DecimalUtils.doubleToIntOrD(mHVVenueOrderFieldBean.getPrice()),mViewHolder.mTvSinglePrice, Constants.PRICE_SIZE_THIRTEEN,Constants.YUAN_SIZE_TEN);
        mViewHolder.mTvSinglePrice.setText(DecimalUtils.setPriceStyle(mContext,mHVVenueOrderFieldBean.getPrice(),R.dimen.price_text_size_15,R.dimen.price_text_size_13,true));
        mViewHolder.mCbCheck.setChecked(sSHELCTED==mHVVenueOrderFieldBean.getMIsSelected());//选中的状态
    }

    @Override
    public void onClick(View v) {
        int pos= (int) v.getTag();
        switch (v.getId()){
            case R.id.cb_zhi:
                CheckBox cb= (CheckBox) v;
                HVVenueOrderFieldBean mHVVenueOrderFieldBean=orderField.get(pos);
                double singlePrice;
                double mCanBackMoney;
                if(cb.isChecked()){//选择退款
                    orderField.get(pos).setMIsSelected(sSHELCTED);
                    singlePrice=mHVVenueOrderFieldBean.getPrice();
                     mSingleBackMonery=mRevertManger.doRevertSingleField(mHVVenueOrderFieldBean,DateUtil.getCurTimeS());
                    mSingDisPrice=singlePrice-mSingleBackMonery;

                    mSelectedRefund.add(mHVVenueOrderFieldBean);
                }else{//取消退款
                    orderField.get(pos).setMIsSelected(sNOSHELCTED);
//                    singlePrice=-mHVVenueOrderFieldBean.getPrice();//取消就减掉

                    singlePrice=mHVVenueOrderFieldBean.getPrice();//单价
                    mSingleBackMonery=mRevertManger.doRevertSingleField(mHVVenueOrderFieldBean,DateUtil.getCurTimeS());//退还的金额
                    mSingDisPrice=singlePrice-mSingleBackMonery;
                    mSingleBackMonery=-mSingleBackMonery;
                    mSingDisPrice=-mSingDisPrice;
//                    mSingleBackMonery=-mSingleBackMonery;

                    mSelectedRefund.remove(mHVVenueOrderFieldBean);
//                    mSingDisPrice=-mSingDisPrice;

                }
                mAllRefundPrice= Arith.add(mAllRefundPrice,mSingleBackMonery);
                mDisPrice= Arith.add(mSingDisPrice,mDisPrice);

//                mAllRefundPrice+=mSingleBackMonery;
//                mDisPrice+=mSingDisPrice;
                notifyDataSetChanged();
                if(null!=mIfChangePrice){
                    mIfChangePrice.refumdPrice(mAllRefundPrice);
                    mIfChangePrice.refumdList(mSelectedRefund);
                    mIfChangePrice.refefunDisPrice(mDisPrice);
                }
                break;
        }
    }

    public void setOrderField(ArrayList<HVVenueOrderFieldBean> orderField) {
        this.orderField = orderField;
    }

    static class RefundHolder{
       private TextView mTvOrderWeek;
       private TextView mTvOrderDay;
       private TextView mTvOrderTime;
       private TextView mTvOrderFieldName;
       private TextView mTvSinglePrice;
       private CheckBox mCbCheck;
   }

}
